package com.xls.commonlibrary.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentResultListener;

import com.orhanobut.logger.Logger;
import com.xls.commonlibrary.R;

import io.reactivex.rxjava3.annotations.NonNull;

// 游戏页面 悬浮按钮
//https://developer.android.google.cn/reference/androidx/fragment/app/DialogFragment
public class GameFragment extends Fragment {

    public final static String CLICK_MINE = "CLICK_MINE";// 标识：点击我的按钮

    ValueCallback<String> valueCallback;

    public void setValueCallback(ValueCallback<String> valueCallback) {
        this.valueCallback = valueCallback;
    }

    TextView tv_my_point;
    TextView tv_goto_mine;

    // Create a new instance of MyDialogFragment, providing "num" as an argument.
    public static GameFragment newInstance(int num) {
        GameFragment f = new GameFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_game, container, false);
        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // https://developer.android.google.cn/guide/fragments/communicate?hl=zh-cn
        // 官方传值新方法：宿主activity或者统一FragmentManager管理的其它Fragment，通知该Fragment的方法
        // old： 该Fragment函数传值，不大好估计
//        Bundle result = new Bundle();
//        result.putString("bundleKey", "数据来自其他Activity或者Fragment");
//        getSupportFragmentManager().setFragmentResult("requestKey", result);

        // FragmentManager 可以充当 fragment 结果的集中存储区
        // 设置结果监听器
        getParentFragmentManager().setFragmentResultListener("requestKey", this, new FragmentResultListener() {
            @Override
            public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle bundle) {
                // We use a String here, but any type that can be put in a Bundle is supported.
                String result = bundle.getString("bundleKey");
                Logger.d("========GameFragment========更新总积分=======================:" + result);

                if (TextUtils.isEmpty(result)){
                    tv_my_point.setText("0");
                }else {
                    // Do something with the result.
                    tv_my_point.setText("" + Long.parseLong(result)/1000);
                }
            }
        });

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        //  views
        initView(view);

        // get args  from Activity or other Fragment
        // getArguments().getString("num");


    }

    void initView(View view) {
        tv_my_point = view.findViewById(R.id.tv_my_point);
        tv_goto_mine = view.findViewById(R.id.tv_goto_mine);


        view.findViewById(R.id.ll_goto_mine).setOnClickListener(v -> {

            // use hook in Activity
            if (valueCallback != null) {
                valueCallback.onReceiveValue(CLICK_MINE);
            }

        });
    }

    // 外部设置本类属性
//    public void setPoint() {
//        Bundle result = new Bundle();
//        result.putString("bundleKey", "数据来自其他Activity或者Fragment");
//        getSupportFragmentManager().setFragmentResult("requestKey", result);
//    }

}
